package com.jie.deletefile.Camera;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.jie.deletefile.Permission.CheckPermission;
import com.jie.deletefile.Permission.PermissionActivity;
import com.jie.deletefile.R;

public class CameraActivity extends AppCompatActivity implements View.OnClickListener {

    private static final int REQEUSE_CODE = 1;//定义没有权限返回码
    //权限集合
    private static final String PERMISSION[] = new String[]{Manifest.permission.CAMERA,};

    private CheckPermission checkPermission;
    private boolean hadpermission;
    private android.widget.Button btncamera;
    private android.widget.RelativeLayout activitycamera;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        this.activitycamera = (RelativeLayout) findViewById(R.id.activity_camera);
        this.btncamera = (Button) findViewById(R.id.btn_camera);
        checkPermission = new CheckPermission(this);
        btncamera.setOnClickListener(this);
        if (checkPermission.permissionSet(PERMISSION)) {
            //如果没有,启动permissionactivity界面,弹出dialog,让用户设置权限
            startPermissionActivity();
        }
        else {
            hadpermission = true;

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //判断是否有调用相机的权限

    }

    private void startPermissionActivity() {
        PermissionActivity.startActivityForResult(this, REQEUSE_CODE, PERMISSION);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //判断是否拿到权限
        if (requestCode == REQEUSE_CODE && requestCode == PermissionActivity.PERMISSION_DENIEG) {
            hadpermission = false;
            return;
        }
        hadpermission = true;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
//        if (hadpermission) {
//            btncamera.setClickable(true);
//        }
    }

    @Override
    public void onClick(View v) {
        if (!hadpermission) {
            //再次半段是否有权限,没有的话,不能使用相机.避免闪退
            Toast.makeText(this, "没有调用相机的权限请设置", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent,300);

    }
}
